#include <iostream>
#include <mutex>
#include "ThreadPool.h"
#include "Runnable.h"
#include "Thread.hpp"
using namespace std;

std::mutex gmutex;

void Print(uint64_t id, int num, const string &state)
{
    gmutex.lock();
    cout << "thread id: " << id << " ,num: " << num << " " << state << endl;
    gmutex.unlock();
}

void Print(int num)
{
    gmutex.lock();
    cout << "num: " << num << " ~Runnable" << endl;
    gmutex.unlock();
}

class MyRunnable: public sh::Runnable
{

public:
    explicit MyRunnable(int num)
        : num(num)
    {}
    ~MyRunnable() override
    {
//        Print(num);
    }
private:
    void run() override
    {
        Print(sh::Thread::currentThreadId(), num, "start");
        sh::Thread::sleep(2);
//        Print(sh::Thread::currentThreadId(), num, "end");
    }
private:
    int num;
};

int main()
{
    cout << "start" << endl;
    sh::ThreadPool threadPool;
    threadPool.setMaxThreadCount(3);

    for (int index = 0; index < 20; index++)
    {
        auto *myRunnable = new MyRunnable(index);
        threadPool.start(myRunnable, 20 - index);
    }
    while (true)
    {

    }
    return 0;
}
